<script type="text/javascript" src="__STATIC__/layui/layui.all.js"></script>
<script type="text/javascript">
    var layer = layui.layer,
        $ = layui.jquery,
        element = layui.element,
        form = layui.form;

    element.on('nav(side)', function (elem) {
        var groupID = {$admin.group_id};
        var href = elem.data('href');

        if (href != null) {
            var rule = href.substr(1);

            $.post("{:url('admin/public/checkAuth')}?rule=" + rule + "&groupID=" + groupID, function (res) {
                if (res == 1) {
                    window.location.href = href;
                }
                else {
                    layer.msg('无权访问');
                }
            });
        }

    });

    $('.dent').click(function () {
        $('.layui-layout').toggleClass('navin');
        $('.layui-nav-tree li.layui-nav-item').toggleClass("layui-nav-itemed");
        var width = $('.layui-side').width();
        if (width == 55) {
            $('.layui-nav-tree li.layui-nav-item').removeClass("layui-nav-itemed");
        } else {
            $('.layui-nav-tree li.layui-nav-item').addClass("layui-nav-itemed");
        }
    });

    /**
     * @author Eric ieyangxiaoguo@126.com
     * @desc 监听switch开关事件
     */
    form.on('switch(switch)', function (data) {
        var url = $(this).data('url');
        var change = data.elem.checked; //开关是否开启，true或者false

        if (change) { //后台我需要的是0或1，所以预先在js中处理change的值
            change = 1;
        } else {
            change = 0;
        }

        $.post(url, {change: change}, function (res) {
            layer.msg(res.msg);
        });
    });

    form.on('submit(general)', function (data) {
        layer.load();
        $.post(data.form.action, data.field, function (res) {
            if (res.code == 1) {
                layer.msg(res.msg, {time: 1000}, function () {
                    if (res.url) {
                        window.location.href = res.url;
                    }
                    else {
                        window.location.reload();
                    }
                });
            }
            else {
                layer.msg(res.msg, {}, function () {
                    layer.closeAll();
                });
            }
        });
        return false;
    });
</script>
